<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ include file="/manage/system/pageBase.jsp" %>
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%>
<%@ page info="角色关系管理" %>

<style>
    table {
        table-layout: fixed; /* 只有定义了表格的布局算法为fixed，下面td的定义才能起作用。 */
        width:100%;
    }

    td {
        word-break: keep-all; /* 不换行 */
        white-space: nowrap; /* 不换行 */
        overflow: hidden; /* 内容超出宽度时隐藏超出部分的内容 */
        text-overflow: ellipsis; /* 当对象内文本溢出时显示省略标记(...) ；需与overflow:hidden;一起使用*/
    }

</style>

    <div style="height:auto!important;height:550px;min-height:550px;">
        <h3 style="border-bottom: 1px solid #D7D7D7;color: #666666;font-size: 28px;padding-bottom: 20px;margin-bottom: 30px;">
            <%=getServletInfo()%>
            <shiro:hasPermission name="roleres:toAdd">
                <a href="toAdd" class="btn btn-primary"
                   style="padding: 2px 15px;float:right;">
                    <i class="icon-plus "></i>添加角色关系
                </a>
            </shiro:hasPermission>
        </h3>
        <table class="table table-bordered table-hover table-striped" >
            <tr >
                <th width="5%">序号</th>
                <th width="20%">角色id</th>
                <th width="20%">页面id</th>
                <th width="20%">资源id</th>
                <th width="20%">创建时间</th>
                <th width="20%">更新时间</th>
                <th width="20%" align="center">操作</th>
            </tr>
            <c:forEach var="item" items="${pager.list}">
                <tr>
                    <td>${item.id}</td>
                    <td>${item.roleid}</td>
                    <td>${item.respageid}</td>
                    <td>${item.resourcesid}</td>
                    <td>${item.createtime}</td>
                    <td>${item.updatetime}</td>
                    <td>
                        <shiro:hasPermission name="roleres:toEdit">
                            <a href="toEdit?id=${item.id}">编辑</a>&ensp;
                        </shiro:hasPermission>
                        <shiro:hasPermission name="roleres:toDel">
                            <a href="toDel?id=${item.id}">删除</a>
                        </shiro:hasPermission>
                    </td>
                </tr>
            </c:forEach>
            <tr>
                <td colspan="71" style="text-align: center;">
                    <%@ include file="/manage/system/page.jsp" %>
                </td>
            </tr>
        </table>
    </div>
<script>
    $(function(){
        var setting = {
            check: {
                enable: true,
                dblClickExpand: false
            },callback: {
                onClick: function(e,treeId, treeNode) {
                    var zTree = $.fn.zTree.getZTreeObj("treeDemo2");
                    zTree.expandNode(treeNode);
                },
                onMouseDown: function (event, treeId, treeNode) {
                    var url = "toAddOrUpdate?id="+treeNode.id;
                    if(true){
                        $("#iframeMenuEdit").attr("src",url);
                        return;
                    }
                    //alert(url);
                    $("#dfsfsf").val(treeNode.id);
                    document.form1.action = url;
                    document.form1.submit();
                }
            },
            data:{
                key:{
                    url:"_url"
                }
            }
        };

        loadMenusTree();

//加载菜单树
        function loadMenusTree(){
            $.ajax({
                url:"<%=path%>/manage/menu/getMenusByPid?pid=0",
                type:"post",
                dataType:"text",
                success:function(data, textStatus){
                    var zNodes = eval('('+data+')');

                    $.fn.zTree.init($("#treeDemo2"), setting, zNodes);
                    $("#loadImg").hide();
                    $("#treeDemo2").show();
                },
                error:function(){
                    alert("error");
                }
            });
        }

        //点击菜单项


        //删除菜单
        $("#deleteMenus").click(function(){

            if(!confirm("确定删除选择的菜单项?")){
                return false;
            }
// 				alert("deleteMenus...");
            var ids = "";
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo2");
            var nodes = treeObj.getCheckedNodes(true);
            if(nodes.length==0){
                return false;
            }
            for(var i=0;i<nodes.length;i++){
// 					alert(nodes[i].id);
                ids+=nodes[i].id+",";
            }

            $.ajax({
                url:"<%=path%>/manage/menu/delete",
                type:"post",
                data:{ids:ids,deleteParent:$("#deleteParent").attr("checked")?"1":"-1"},
                dataType:"text",
                success:function(data){
// 						var zNodes = eval('('+data+')');
// 						$.fn.zTree.init($("#treeDemo2"), setting, zNodes);
                    if(data==1){
                        loadMenusTree();
                    }else{
                        alert("删除菜单失败！");
                    }
                },
                error:function(){
                    alert("删除菜单失败！");
                }
            });
        });


        var expandAllFlg = true;
        function expandNode(e) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo2"),
                type = e.data.type,
                nodes = zTree.getSelectedNodes();

            if (type == "expandAll") {
                zTree.expandAll(true);
            } else if (type == "collapseAll") {
                zTree.expandAll(false);
            } else if (type == "expandOrCollapse") {
                zTree.expandAll(expandAllFlg);
                expandAllFlg = !expandAllFlg;
            } else {
                if (type.indexOf("All")<0 && nodes.length == 0) {
                    alert("请先选择一个父节点");
                }
                var callbackFlag = $("#callbackTrigger").attr("checked");
                for (var i=0, l=nodes.length; i<l; i++) {
                    zTree.setting.view.fontCss = {};
                    if (type == "expand") {
                        zTree.expandNode(nodes[i], true, null, null, callbackFlag);
                    } else if (type == "collapse") {
                        zTree.expandNode(nodes[i], false, null, null, callbackFlag);
                    } else if (type == "toggle") {
                        zTree.expandNode(nodes[i], null, null, null, callbackFlag);
                    } else if (type == "expandSon") {
                        zTree.expandNode(nodes[i], true, true, null, callbackFlag);
                    } else if (type == "collapseSon") {
                        zTree.expandNode(nodes[i], false, true, null, callbackFlag);
                    }
                }
            }
        }
        $("#expandOrCollapseAllBtn").bind("click", {type:"expandOrCollapse"}, expandNode);
    });
</script>
<%@ include file="/manage/system/baseFoot.jsp" %>